#include "qgscene.h"
#include "qlab1picture.h"
#include "plabel.h"
#include <QBuffer>



QGScene::QGScene(QObject *parent)
	: QGraphicsScene(parent)
{
mdbprint_elementPos.clear();
mdbprint_elementImagePos.clear();
m_stat_update=false;
}

QGScene::~QGScene()
{
}


void QGScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	QGraphicsScene::mousePressEvent(event);
}


void QGScene::setRC(int acol,int arow)
{
e_col=acol;
e_row=arow;
}


void QGScene::InitilizeItems()
{
	double w=(double)750/(double)e_col;
	double h=(double)1000/(double)e_row;

        QList<QGraphicsItem *> itemList=items();

	double x,y;
	int id;
        for(int xc=0;xc<itemList.size();xc++)
		{
		if(qgraphicsitem_cast<QLab1Picture*>(itemList.at(xc)))
		{
			x=((QLab1Picture*)itemList.at(xc))->pos().rx();
			y=((QLab1Picture*)itemList.at(xc))->pos().ry();
			id=((QLab1Picture*)itemList.at(xc))->GetId();

			QRectF r=((QLab1Picture*)itemList.at(xc))->boundingRect();
			double hdown=(double)r.height();
			double wleft=(double)r.width();
			for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)
			{
				if((*idbprint_elementPos)->id==id)
				{
					(*idbprint_elementPos)->m_x=(w/2+x)/w;
					(*idbprint_elementPos)->m_y=(h/2+y+hdown/2)/h;
					(*idbprint_elementPos)->m_width=wleft/w;
					(*idbprint_elementPos)->m_height=hdown/h;
				}
			}
		}
		else if(qgraphicsitem_cast<QLab1PixmapItem*>(itemList.at(xc)))
		{
			x=((QLab1PixmapItem*)itemList.at(xc))->pos().rx();
			y=((QLab1PixmapItem*)itemList.at(xc))->pos().ry();
			id=((QLab1PixmapItem*)itemList.at(xc))->GetId();

			QRectF r=((QLab1PixmapItem*)itemList.at(xc))->boundingRect();
			double hdown=(double)r.height();

			for(idbprint_elementImagePos=mdbprint_elementImagePos.begin();idbprint_elementImagePos<mdbprint_elementImagePos.end();idbprint_elementImagePos++)
			{
				if((*idbprint_elementImagePos)->id==id)
				{
// 					(*idbprint_elementImagePos)->m_x=(w/2+x)/w;
// 					(*idbprint_elementImagePos)->m_y=(h/2+y+hdown/2)/h;
				}
			}

		}

		}
}

void QGScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	double w=(double)750/(double)e_col;
	double h=(double)1000/(double)e_row;

	QList<QGraphicsItem *> itemList=selectedItems();
	double x,y;
	int id;
	foreach (QGraphicsItem *item, itemList) 
	{
		if(qgraphicsitem_cast<QLab1Picture*>(item))
		{
		
			x=((QLab1Picture*)item)->pos().rx();
			y=((QLab1Picture*)item)->pos().ry();
			id=((QLab1Picture*)item)->GetId();

			QRectF r=((QLab1Picture*)item)->boundingRect();
			double hdown=(double)r.height();
			//item->rotate(90);

			
			for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)
			{
				if((*idbprint_elementPos)->id==id)
				{
					(*idbprint_elementPos)->m_x=(w/2+x)/w;
					(*idbprint_elementPos)->m_y=(h/2+y+hdown/2)/h;
				}
			}
		}
		else if(qgraphicsitem_cast<QLab1PixmapItem*>(item))
		{
			x=((QLab1PixmapItem*)item)->pos().rx();
			y=((QLab1PixmapItem*)item)->pos().ry();
			id=((QLab1PixmapItem*)item)->GetId();

			QRectF r=((QLab1PixmapItem*)item)->boundingRect();
			double hdown=(double)r.height();
			//item->rotate(90);


			for(idbprint_elementImagePos=mdbprint_elementImagePos.begin();idbprint_elementImagePos<mdbprint_elementImagePos.end();idbprint_elementImagePos++)
			{
				if((*idbprint_elementImagePos)->id==id)
				{
					(*idbprint_elementImagePos)->m_x=(w/2+x)/w;
					(*idbprint_elementImagePos)->m_y=(h/2+y+hdown/2)/h;
				}
			}
		}


	}
	m_stat_update=true;
	QGraphicsScene::mouseReleaseEvent(event);
}

void QGScene::GetNewPosition(Typemdbprint_elementPos *x)
{
x=&mdbprint_elementPos;
}

void QGScene::deleteSelectedItem()
{
	   QList <QGraphicsItem*> itemList=selectedItems();
		if(!itemList.isEmpty())
		{
			if(qgraphicsitem_cast<QLab1PixmapItem*>(itemList.at(0)))
			{
				itemList.at(0);	
				int id=((QLab1PixmapItem*)itemList.at(0))->GetId();	
				   				

				for(idbprint_elementImagePos=mdbprint_elementImagePos.begin();idbprint_elementImagePos<mdbprint_elementImagePos.end();idbprint_elementImagePos++)
				{
					if((*idbprint_elementImagePos)->id==id)
					{
						mdbprint_elementImagePos.erase(idbprint_elementImagePos);
					}
				}
				delete itemList.at(0); 
			}
		}
}
void QGScene::addInfo(int aid,int ax,int ay,QColor m_color)
{
		newprint_elementPos=new dbprint_elementPos;
		newprint_elementPos->id=aid;	
		newprint_elementPos->m_x=ax;	
		newprint_elementPos->m_y=ay;	
		newprint_elementPos->m_color=m_color;
		mdbprint_elementPos.push_back(newprint_elementPos);
}

double QGScene::GetInfoX(int aid)
{
double x=0;
	for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)	
	{
		if((*idbprint_elementPos)->id==aid)
		{
			x=(*idbprint_elementPos)->m_x;
		}
	}
	return x;
}

QColor QGScene::getColor(int aid)
{
	QColor x;
	for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)	
	{
		if((*idbprint_elementPos)->id==aid)
		{
			x=(*idbprint_elementPos)->m_color;
		}
	}
	return x;
}

double QGScene::GetInfoY(int aid)
{
	double y=0;
	double h=(double)1000/(double)e_row;
	for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)	
	{
		if((*idbprint_elementPos)->id==aid)
		{
			y=(*idbprint_elementPos)->m_y;
		}
	}
	return y;
}


void QGScene::addInfoImage(int aid,double ax,double ay,QByteArray m_data)
{
	newprint_elementImagePos=new dbprint_elementImagePos;
	newprint_elementImagePos->id=aid;	
	newprint_elementImagePos->m_x=ax;	
	newprint_elementImagePos->m_y=ay;	
	newprint_elementImagePos->m_data=m_data;
	mdbprint_elementImagePos.push_back(newprint_elementImagePos);
}

double QGScene::GetInfoImageX(int aid)
{
	double x=0;
	for(idbprint_elementImagePos=mdbprint_elementImagePos.begin();idbprint_elementImagePos<mdbprint_elementImagePos.end();idbprint_elementImagePos++)	
	{
		if((*idbprint_elementImagePos)->id==aid)
		{
			x=(*idbprint_elementImagePos)->m_x;
		}
	}
	return x;
}
double QGScene::GetInfoImageY(int aid)
{
	double y=0;
	for(idbprint_elementImagePos=mdbprint_elementImagePos.begin();idbprint_elementImagePos<mdbprint_elementImagePos.end();idbprint_elementImagePos++)	
	{
		if((*idbprint_elementImagePos)->id==aid)
		{
			y=(*idbprint_elementImagePos)->m_y;
		}
	}
	return y;
}



void QGScene::scaleSelectedItem(double scale)
{

	QList <QGraphicsItem*> itemList=selectedItems();
	if(!itemList.isEmpty())
	{
		if(qgraphicsitem_cast<QLab1PixmapItem*>(itemList.at(0)))
		{
			itemList.at(0);	
			int id=((QLab1PixmapItem*)itemList.at(0))->GetId();	



			for(idbprint_elementImagePos=mdbprint_elementImagePos.begin();idbprint_elementImagePos<mdbprint_elementImagePos.end();idbprint_elementImagePos++)
			{
				if((*idbprint_elementImagePos)->id==id)
				{
					QPixmap temp;
                    temp.loadFromData((*idbprint_elementImagePos)->m_data,"PNG",Qt::AutoColor);
					temp=temp.scaled(temp.width()/scale,temp.height()/scale,Qt::IgnoreAspectRatio,Qt::FastTransformation);

					QByteArray bytes;
					QBuffer buffer(&bytes);
					buffer.open(QIODevice::WriteOnly);
                                        temp.save(&buffer,"PNG"); // writes pixmap into bytes in PNG format
					(*idbprint_elementImagePos)->m_data=bytes;
					((QLab1PixmapItem*)itemList.at(0))->setPixmap(temp);
			
				}
			}
			
		}
	}
}


void QGScene::setColorText(QColor m_color)
{

	QList <QGraphicsItem*> itemList=selectedItems();
	if(!itemList.isEmpty())
	{
		if(qgraphicsitem_cast<QLab1Picture*>(itemList.at(0)))
		{
			itemList.at(0);	
			int id=((QLab1Picture*)itemList.at(0))->GetId();	
			((QLab1Picture*)itemList.at(0))->setDefaultTextColor(m_color);

			for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)
			{
				if((*idbprint_elementPos)->id==id)
				{
					(*idbprint_elementPos)->m_color=m_color;
				}
			}
		}
	}
}


double QGScene::getWidth(int aid)
{
	double x=0;
	for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)	
	{
		if((*idbprint_elementPos)->id==aid)
		{
			x=(*idbprint_elementPos)->m_width;
		}
	}
	return x;
}

double QGScene::getHeight(int aid)
{
	double x=0;
	for(idbprint_elementPos=mdbprint_elementPos.begin();idbprint_elementPos<mdbprint_elementPos.end();idbprint_elementPos++)	
	{
		if((*idbprint_elementPos)->id==aid)
		{
			x=(*idbprint_elementPos)->m_height;
		}
	}
	return x;
}

void QGScene::autoSelectedItem()
{
	QList <QGraphicsItem*> itemList=selectedItems();
	QList <QGraphicsItem*>::iterator iter;
	if(!itemList.isEmpty())
	{
// 		if(qgraphicsitem_cast<QLab1Picture*>(itemList.at(0)))
		{
			qreal x =((QLab1Picture*)itemList.at(0))->pos().x();	
			qreal y =((QLab1Picture*)itemList.at(0))->pos().y();	
//			foreach (QGraphicsItem *item, itemList) 
			for(iter=itemList.begin();iter<itemList.end();iter++)
			{
// 				if(qgraphicsitem_cast<QLab1Picture*>(*iter))
				{

					(*iter)->setPos(x,(*iter)->pos().y());
					//y+=(*iter)->boundingRect().height()*3/4;
				}
			}
		}
	}
	
}